package com.nb82.bean.weather.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.nb82.bean.weather.HTTPUtil;
import com.nb82.bean.weather.WeatherUtil;
import com.nb82.bean.weather.codeEnum.WeatherCodeEnum;
import com.nb82.bean.weather.codeEnum.WindDirectionEnum;
import com.nb82.bean.weather.codeEnum.WindEnum;
import com.nb82.bean.weather.entity.Index;
import com.nb82.bean.weather.entity.InitParams;
import com.nb82.bean.weather.entity.NowWeather;
import com.nb82.bean.weather.entity.Regular;

/**
 * this is SmartWeatherService {@code getWeatherToString()}
 * 
 * @author cxd
 * 
 */
public class SmartWeatherService {
	private HTTPUtil httpUtil;

	public static void main(String[] args) {
		SmartWeatherService service = new SmartWeatherService();
		InitParams initParams = new InitParams();
		initParams.setAreaid("101010100");
		initParams.setType(Type.forecast3d.getType());
		service.getWeatherToString(initParams);
	}

	public String getWeatherToString(InitParams params) {
		httpUtil = HTTPUtil.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
		Date date = new Date(System.currentTimeMillis());
		String dateTime = dateFormat.format(date);
		String str = null;
		try {
			str = httpUtil.httpGet(WeatherUtil.getInterfaceURL(
					params.getAreaid(), params.getType(), dateTime));
			System.out.println(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public Object getWeather(InitParams params) {
		try {
			String str = getWeatherToString(params);
			String type = params.getType();
			JSONObject object = new JSONObject(str);
			if (type.equals(Type.observe.getType())) {
				if (!object.isNull("l")) {
					Map<String, Object> map = new HashMap<String, Object>();
					JSONObject obj = object.getJSONObject("l");
					NowWeather nowWeather = new NowWeather();
					nowWeather.setNowTem(obj.getString("l1"));
					nowWeather.setNowHumidity(obj.getString("l2"));

					nowWeather.setNowWind(obj.getString("l3"));
					nowWeather.setNowWindNum(obj.getString("l4"));

					nowWeather.setTime(obj.getString("l7"));

					map.put("nowWeather", nowWeather);
					map.put("windEnum", WindEnum.getWindEnum(Integer
							.valueOf(obj.getString("l3"))));
					map.put("WindDirectionEnum", WindDirectionEnum
							.getWindDirectionEnum(Integer.valueOf(obj
									.getString("l4"))));
					return map;
				}
			}
			// {c:{},f{f1:[{},{}],f0:time}}
			if (type.equals(Type.forecast3d.getType()) && !object.isNull("c")
					&& !object.isNull("f")) {
				JSONObject c = object.getJSONObject("c");
				JSONObject f = object.getJSONObject("f");
				JSONArray f1 = f.getJSONArray("f1");
				if (f1 != null) {
					List<Regular> list = new ArrayList<Regular>();
					for (int i = 0; i < f1.length(); i++) {
						JSONObject obj = f1.getJSONObject(i);
						Regular regular = new Regular();
						regular.setAreaId(c.getString("c1"));
						regular.setCityEn(c.getString("c2"));
						regular.setCityZn(c.getString("c3"));
						regular.setCityLv(c.getString("c10"));
						regular.setCityZoneCode(c.getString("c11"));
						regular.setCityZipCode(c.getString("c12"));
						regular.setLongitude(c.getString("c13"));
						regular.setLatitude(c.getString("c14"));
						regular.setAltitude(c.getString("c15"));
						regular.setRadarId(c.getString("c16"));

						regular.setTime(f.getString("f0"));
						regular.setDayWeather(WeatherCodeEnum.get(obj
								.getString("fa")));
						regular.setNightWeather(WeatherCodeEnum.get(obj
								.getString("fb")));
						regular.setDayTem(obj.getString("fc"));
						regular.setNightTem(obj.getString("fd"));
						if (!obj.getString("fe").equals("")) {
							regular.setDayWindNum(WindDirectionEnum
									.getWindDirectionEnum(Integer.valueOf(obj.getString("fe"))));

						}
						if (!obj.getString("ff").equals("")) {
							regular.setNightWindNum(WindDirectionEnum
									.getWindDirectionEnum(Integer.valueOf(obj
											.getString("ff"))));
						}
						if (! obj.getString("fg").equals("")) {
							regular.setDayWind(WindEnum.getWindEnum(Integer
									.valueOf(obj.getString("fg"))));
						}
						if (! obj.getString("fh").equals("")) {

							regular.setNightWind(WindEnum.getWindEnum(Integer
									.valueOf(obj.getString("fh"))));
						}
						regular.setSunriseAndSundown(obj.getString("fi"));
						
						list.add(regular);
					}
					return list;
				}
			}
			if (type.equals(Type.index.getType()) && !object.isNull("i")) {
				JSONArray array = object.getJSONArray("i");
				// {"i1":"ct","i2":"ç©¿è¡£ææ°","i3":"","i4":"èé","i5":"å»ºè®®çé¿è¢Tæ¤ãè¡¬è¡«å åè£¤ç­æè£ãå¹´èä½å¼±èå®çéç»é¿è¢è¡¬è¡«ãé©¬ç²åé¿è£¤ã"}
				List<Index> indexs = new ArrayList<Index>();
				for (int i = 0; i < array.length(); i++) {
					JSONObject obj = array.getJSONObject(i);
					Index index = new Index();
					index.setIndexShort(obj.getString("i1"));
					index.setIndexZn(obj.getString("i2"));
					index.setIndexZnOther(obj.getString("i3"));
					index.setIndexLv(obj.getString("i4"));
					index.setIndexContent(obj.getString("i5"));
					indexs.add(index);
				}
				return indexs;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static Gson gson;

	public Gson getGson() {
		return gson != null ? gson : new Gson();
	}

	public enum Type {
		alarm("alarm"), observe("observe"), index("index"), forecast3d(
				"forecast3d");
		private final String type;

		private Type(String type) {
			this.type = type;
		}

		public String getType() {
			return type;
		}
	}
}
